import os
import boto3
from configuration import get_boto3_client

def publish_js():
    """
    上传当前目录的所有js文件到S3
    """
    # 获取当前目录下的所有js文件
    current_dir = os.getcwd()
    js_files = []
    
    for file in os.listdir(current_dir):
        if file.endswith('.js'):
            js_files.append(file)
    
    if not js_files:
        print("当前目录下没有找到JavaScript文件")
        return
    
    # 显示要上传的js文件
    print("将要上传的JavaScript文件：")
    for file in js_files:
        print(f"- {file}")
    
    print(f"\n开始上传 {len(js_files)} 个文件...")
    
    try:
        # 上传到S3
        s3_client = get_boto3_client('s3')
        bucket_name = 'zhuchundashuaige'
        
        success_count = 0
        for js_file in js_files:
            s3_key = f'public/js/{js_file}'
            file_path = os.path.join(current_dir, js_file)
            
            print(f"上传: {js_file} -> s3://{bucket_name}/{s3_key}")
            s3_client.upload_file(file_path, bucket_name, s3_key)
            success_count += 1
            print(f"✅ 成功上传 {js_file}")
        
        print(f"\n🎉 完成！成功上传 {success_count}/{len(js_files)} 个文件到 s3://{bucket_name}/public/js/")
        
    except Exception as e:
        print(f"❌ 错误: {str(e)}")

if __name__ == "__main__":
    publish_js()
